/**
 * 
 */

var chestr="";
var totalnum=0;
var callback=1;

function initOption(){
  $.ajax({
	  url:'KClassAction!selectKClassList',
			dataType:'json',
			type:'post',
			success:function(data)
			{
				getAllChecked();
				$("#opList").empty();
				  $("#opList").append('<option onclick="initHtml()">全部课程</option>');
				for(var i=0;i<data.kClassList.length;i++)
					{
					   $("#opList").append('<option  onclick="changeOptionValue('+data.kClassList[i].id+')" >'+data.kClassList[i].name+'</option>');
					}
			}
  });
}
  function initHtml(pageNo){
	$.ajax({
		url:'KGradeAction!selectKGradeListPass',
		data:{'pageNo':pageNo},
		dataType:'json',
		type:'post',
    	success:function(data)
			{
    		    getAllChecked(); 
    		    var count=totalnum;
    		    totalnum=data.paginationKGrade.totalPage;
    		   if(data.paginationKGrade.totalPage!=count)
    		       initPagination();
    		 
				 $("#Cbody").empty();
				for(var i=0;i<data.paginationKGrade.list.length;i++)
					{
					   var bd = new Date(data.paginationKGrade.list[i].gbeginttime);
					   var ed= new Date(data.paginationKGrade.list[i].gendtime);
					   var bdate=bd.getFullYear() + " " + (bd.getMonth()+1) + " " + bd.getDate();
					   var edate=ed.getFullYear() + " " + (ed.getMonth()+1) + " " + ed.getDate();
			          $("#Cbody").append('<tr><td><a href="#">'+data.paginationKGrade.list[i].gname+'</a></td>'
			        		 +'<td>'+bdate+'-<br/>-'+edate+'</td>'
			        		 +'<td>'+data.paginationKGrade.list[i].gexpress+'</td> <td><input type="checkbox" name="box" value="'+data.paginationKGrade.list[i].gid+'"></td>'
			        		 +'</tr>');
					}
				setChecked();
			}
		});
  }
	function change(data)
	{
		getAllChecked();
		  
		$.ajax({
			url:'KGradeAction!selectKGradeweek',
			data:{'pageNo':1,"weekNumber":data},
			dataType:'json',
			type:'post',
			success:function(data)
			{
				   
				 
	    		    totalnum=data.paginationKGrade.totalPage;
	    		    initPaginationB();
				 $("#Cbody").empty();
					for(var i=0;i<data.paginationKGrade.list.length;i++)
					{
						  var bd = new Date(data.paginationKGrade.list[i].gbeginttime);
						   var ed= new Date(data.paginationKGrade.list[i].gendtime);
						   var bdate=bd.getFullYear() + " " + (bd.getMonth()+1) + " " + bd.getDate();
						   var edate=ed.getFullYear() + " " + (ed.getMonth()+1) + " " + ed.getDate();
				          $("#Cbody").append('<tr><td><a href="#">'+data.paginationKGrade.list[i].gname+'</a></td>'
				        		 +'<td>'+bdate+'-<br/>-'+edate+'</td>'
				        		 +'<td>'+data.paginationKGrade.list[i].gexpress+'</td> <td><input type="checkbox" name="box" value="'+data.paginationKGrade.list[i].gid+'"></td>'
				        		 +'</tr>');
					}
					setChecked();
			}
		});
	}
	
	function changeOptionValue(id)
	{
		getAllChecked();
		$.ajax({
			url:'KGradeAction!selectKGradeClassId',
			data:{'pageNo':1,"ClassId":id},
			dataType:'json',
			type:'post',
			success:function(data)
			{
				totalnum=data.paginationKGrade.totalPage;
    		    initPaginationB();
				 $("#Cbody").empty();
				
					for(var i=0;i<data.paginationKGrade.list.length;i++)
					{
						  var bd = new Date(data.paginationKGrade.list[i].gbeginttime);
						   var ed= new Date(data.paginationKGrade.list[i].gendtime);
						   var bdate=bd.getFullYear() + " " + (bd.getMonth()+1) + " " + bd.getDate();
						   var edate=ed.getFullYear() + " " + (ed.getMonth()+1) + " " + ed.getDate();
				          $("#Cbody").append('<tr><td><a href="#">'+data.paginationKGrade.list[i].gname+'</a></td>'
				        		 +'<td>'+bdate+'-<br/>-'+edate+'</td>'
				        		 +'<td>'+data.paginationKGrade.list[i].gexpress+'</td> <td><input type="checkbox" name="box"value="'+data.paginationKGrade.list[i].gid+'"></td>'
				        		 +'</tr>');
					}
					setChecked();
			}
		});
	
	}
	
	
function  getAllChecked()
{
	var str=document.getElementsByName("box");
	var objarray=str.length;

	for (i=0;i<objarray;i++)
	  if(str[i].checked == true)
	  {
	      chestr+=str[i].value+",";
	  }
	//alert(" all"+chestr);
}

function setChecked()
{
	var str=document.getElementsByName("box");
	var objarray=str.length;
	var che= new Array();
     che =chestr.split(",");
     chestr="";
      //alert("split  "+che);
for (i=0;i<objarray;i++)
	for(j=0;j<che.length;j++)
	  {		
		if(str[i].value== che[j])
		 {
			//  alert("same "+che[j]);
			  str[i].checked = true;
		      che.splice(j,1);
			  break;
          }
	}
      chestr=che.join(",");
     //  alert("new "+chestr);
}
function submitToOrder()
{
	
	getAllChecked();
	if(chestr=="")
		alert("请先选择");
	else{
	$.ajax({
		url:'order/AddOrder',
		data:{"chestr":chestr},
		dataType:'json',
		type:'post',
		success:function(data)
		{
			 location.href="student/sub9";
		},
		error:function()
		{
			alert("500：服务器内部错误");
		}
	});
	}
}

 function initPagination() {
		//var num_entries = $("#hiddenresult div.result").length;
		// 创建分页
		$("#Pagination").pagination(totalnum, {
			num_edge_entries: 1, //边缘页数
			num_display_entries: 4, //主体页数
			callback: pageselectCallback,
			items_per_page: 1, //每页显示1项
			prev_text: "前一页",
			next_text: "后一页"
		});
	 }
 function initPaginationB() {
		//var num_entries = $("#hiddenresult div.result").length;
		// 创建分页
		$("#Pagination").pagination(totalnum, {
			num_edge_entries: 1, //边缘页数
			num_display_entries: 4, //主体页数
			callback: pagenoselectCallback,
			items_per_page: 1, //每页显示1项
			prev_text: "前一页",
			next_text: "后一页"
		});
	 }
	function pageselectCallback(page_index, jq){
	/*	var new_content = $("#hiddenresult div.result:eq("+page_index+")").clone();
		$("#Searchresult").empty().append(new_content); //装载对应分页的内容
		
*/	
         page_index+=1;    
        	 (callback==1)? callback=0:initHtml(page_index); 
		return false;
	}
	function pagenoselectCallback(page_index, jq)
	{
		return false;
	}
initHtml(1);
initOption();